package com.example.a79273.shandianban;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.a79273.shandianban.config.NetConfig_DesignerInfo;
import com.example.a79273.shandianban.fragment.CommentFragment;
import com.example.a79273.shandianban.fragment.DesignerDetail;
import com.example.a79273.shandianban.fragment.Product;

public class DesignerInfoActivity extends BaseActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {

    private RadioGroup mRadioGroup;
    private RadioButton detail_rb, comment_rb, product_rb;
    private ImageView back_iv, share_iv;

    private FragmentManager fragmentManager = getSupportFragmentManager();
    private Fragment currentFragment;
    private String designerId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_designer_info);
        initView();
        initData();
        setData();
        setListener();
    }

    private void setListener() {
        back_iv.setOnClickListener(this);
        share_iv.setOnClickListener(this);
        mRadioGroup.setOnCheckedChangeListener(this);
    }

    private void setData() {

    }

    private void initData() {
        //获取设计师id
        Intent intent = getIntent();
        if (intent != null) {
            designerId = intent.getStringExtra("designerId");
        }


        //选项颜色
        for (int i = 0; i < mRadioGroup.getChildCount(); i++) {
            RadioButton child = (RadioButton) mRadioGroup.getChildAt(i);
            if (i == 0) {
                child.setTextColor(Color.BLACK);
                continue;
            }
            child.setTextColor(Color.GRAY);
        }
        //初始加载的fragment
        showAndHideOfFragment(DesignerDetail.class);
    }

    private void initView() {
        back_iv = (ImageView) findViewById(R.id.iv_designer_back);
        share_iv = (ImageView) findViewById(R.id.iv_designer_share);
        mRadioGroup = (RadioGroup) findViewById(R.id.rg_designer_buttons);
        detail_rb = (RadioButton) findViewById(R.id.rb_designer_detail);
        comment_rb = (RadioButton) findViewById(R.id.rb_designer_comment);
        product_rb = (RadioButton) findViewById(R.id.rb_designer_product);
    }

    /*加载Fragment*/
    private void showAndHideOfFragment(Class clazz) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        Fragment fragmentByTag = fragmentManager.findFragmentByTag(clazz.getSimpleName());
        if (fragmentByTag == null) {
            try {
                fragmentByTag = (Fragment) clazz.newInstance();
                if (!TextUtils.isEmpty(designerId)) {
                    Bundle bundle = new Bundle();
                    String url = null;
                    switch (clazz.getSimpleName()) {
                        case "DesignerDetail":
                            url = String.format(NetConfig_DesignerInfo.DETAIL_URL, designerId);
                            break;
                        case "CommentFragment":
                            url = String.format(NetConfig_DesignerInfo.COMMENT_URL, designerId);
                            break;
                        case "Product":
                            //TODO
                            break;
                    }
                    bundle.putString("url", url);
                    fragmentByTag.setArguments(bundle);
                }
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            transaction.add(R.id.fl_designer_occupied, fragmentByTag, clazz.getSimpleName());
        }

        transaction.show(fragmentByTag);
        if (currentFragment != null) {
            transaction.hide(currentFragment);
        }
        currentFragment = fragmentByTag;

        transaction.commit();
    }

    @Override
    public void onClick(View v) {
        if (v != null) {
            switch (v.getId()) {
                case R.id.iv_designer_back:
                    finish();
                    break;
                case R.id.iv_designer_share:
                    //TODO,分享
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        Class clazz = null;
        switch (checkedId) {
            case R.id.rb_designer_detail:
                clazz = DesignerDetail.class;
                break;
            case R.id.rb_designer_comment:
                clazz = CommentFragment.class;
                break;
            case R.id.rb_designer_product:
                clazz = Product.class;
                break;
        }
        for (int i = 0; i < group.getChildCount(); i++) {
            RadioButton child = (RadioButton) group.getChildAt(i);

            if (checkedId == child.getId()) {
                child.setTextColor(Color.BLACK);
                continue;
            }
            child.setTextColor(Color.GRAY);
        }
        showAndHideOfFragment(clazz);
    }
}
